package org.iteam.xlz.manager.action.order;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.Random;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.iteam.core.utils.DateUtil;
import org.iteam.web.struts.action.AbstractModuleAction;
import org.iteam.web.struts.action.RestAction;
import org.iteam.xlz.core.entity.order.OrderInfo;
import org.iteam.xlz.core.service.order.OrderInfoService;
import org.iteam.xlz.core.service.product.ProductTypeService;

@SuppressWarnings("unchecked")
@Results({@Result(name="INDEX",type="freemarker",location="/WEB-INF/content/order/info/index.html"),
	      @Result(name="DOWNLOAD",type="stream",params={"contentType","application/octet-stream",
	    		  "inputName","inputStream","contentDisposition","attachment;filename=${fileName}",
	    		  "bufferSize","4096"})})
public class OrderInfoAction extends AbstractModuleAction<OrderInfo> implements RestAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = -7266635164406448036L;
	private final String PATH="/order/info";
	
	@Override
	public OrderInfo getModel() {
		if(null==model){
			model=new OrderInfo();
		}
		return model;
	}

	@Override
	@Action(PATH+"/index")
	public String index() {
		map.put("typeTree",ProductTypeService.getInstance().getTypeTree());		
		return INDEX;
	}

	@Override
	@Action(PATH+"/query")
	public String query() {
		map.put("total",OrderInfoService.getInstance().counts(spid,ptid,productname,orderStatus,_mobile,
				terminal,leftCreateTime,rightCreateTime,leftPayTime,rightPayTime));
		map.put("rows",OrderInfoService.getInstance().query(spid,ptid,productname,orderStatus,_mobile,
				terminal,leftCreateTime,rightCreateTime,leftPayTime,rightPayTime,page,rows,true));
		return MAP;
	}

	@Override
	public String create() {
		return null;
	}

	@Override
	public String update() {
		return null;
	}

	@Override
	public String delete() {
		return null;
	}

	@Override
	@Action(PATH+"/download")
	public String destroy() {
		setInputStream();
		return "DOWNLOAD";
	}
	
	public InputStream getInputStream() {
		return new ByteArrayInputStream(bytes);
	}
	
	private void setInputStream(){
		List<OrderInfo> list=OrderInfoService.getInstance().query(spid,ptid,productname,orderStatus,
			_mobile,terminal,leftCreateTime,rightCreateTime,leftPayTime,rightPayTime,page,rows,false);
		// 第一步，创建一个webbook，对应一个Excel文件
		HSSFWorkbook wb = new HSSFWorkbook();
		// 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
		HSSFSheet sheet = wb.createSheet("订单统计表");
		// 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
		HSSFRow row = sheet.createRow((int) 0);
		// 第四步，创建单元格
		//订单编号oid,手机号码uid,商品库存名称,商品上架名称ppid,票券编号tid,充值编号rid,交易号tradeNo,交易类型ordertype,总金额totalprice,数量amount,支付方式paytype,订单状态status,支付时间paytime,第三方流水号thirdid,创建时间createtime
		HSSFCell cell = row.createCell(0);
		cell.setCellValue("订单编号");
		cell = row.createCell(1);
		cell.setCellValue("手机号码");
		cell = row.createCell(2);
		cell.setCellValue("商品销售名称");
		cell = row.createCell(3);
		cell.setCellValue("交易类型");
		cell = row.createCell(4);
		cell.setCellValue("总金额");
		cell = row.createCell(5);
		cell.setCellValue("数量");
		cell = row.createCell(6);
		cell.setCellValue("联系人");
		cell = row.createCell(7);
		cell.setCellValue("联系电话");
		cell = row.createCell(8);
		cell.setCellValue("收货地址");
		cell = row.createCell(9);
		cell.setCellValue("支付方式");
		cell = row.createCell(10);
		cell.setCellValue("支付时间");
		cell = row.createCell(11);
		cell.setCellValue("订单状态");
		cell = row.createCell(12);
		cell.setCellValue("创建时间");

		// 第五步，写入实体数据 实际应用中这些数据从数据库得到，
		for(int i=1;i<=list.size();i++){
			row = sheet.createRow(i);
			row.createCell(0).setCellValue(list.get(i-1).getOid());
			row.createCell(1).setCellValue(list.get(i-1).getUid());
			row.createCell(2).setCellValue(list.get(i-1).getPid());
			int j=list.get(i-1).getOrdertype();
			String value=j+"";
			if(j==0){
				value="商品交易(票券交易)";
			}else
				if(j==1){
					value="会员卡充值(充值卡充值)";
				}else
					if(j==2){
						value="道乐充值(充值卡充值)";
					}else
						if(j==3){
							value="商品配送交易(物流配送)";
						}
			row.createCell(3).setCellValue(value);
			row.createCell(4).setCellValue(list.get(i-1).getTotalprice());
			row.createCell(5).setCellValue(list.get(i-1).getAmount());
			j=list.get(i-1).getPaytype();
			value=j+"";
			if(j==-1){
				value="未知";
			}else
				if(j==0){
					value="票劵";
				}else
					if(j==1){
						value="微信支付";
					}else
						if(j==2){
							value="会员卡支付";
						}else
							if(j==3){
								value="支付宝支付";
							}else
								if(j==4){
									value="银联支付";
								}else
									if(j==5){
										value="道乐支付";
									}
			row.createCell(6).setCellValue(list.get(i-1).getLinkman());
			row.createCell(7).setCellValue(list.get(i-1).getMobile());
			row.createCell(8).setCellValue(list.get(i-1).getAddr());
			row.createCell(9).setCellValue(value);
			row.createCell(10).setCellValue(list.get(i-1).getPayTime());
			j=list.get(i-1).getStatus();
			if(j==0){
				value="待付款";
			}else
				if(j==1){
					value="待收货";
				}else
					if(j==2){
						value="已完成";
					}else
						if(j==3){
							value="退款/货关闭";
						}else					
							if(j==4){
								value="订单关闭";
							}
			row.createCell(11).setCellValue(value);
			row.createCell(12).setCellValue(list.get(i-1).getCreatetime());
		}
		// 第六步，将文件存到指定位置
		try
		{
			ByteArrayOutputStream  os=new ByteArrayOutputStream();
			wb.write(os);
			bytes=os.toByteArray();
			fileName = DateUtil.formatDate(new Date()) + new Random().nextInt(999)+".xls";//这里是给excel文件设置文件名（时间+一个随机数）
			wb.close();
		}catch (Exception e){
			e.printStackTrace();
		}
	}

	@Override
	public String edit() {
		return null;
	}

	@Override
	public String editNew() {
		return null;
	}
	
	private String spid;
	private String productname;
	private String orderStatus;
	private String _mobile;
	private String terminal;
	private String ptid;
	private String leftCreateTime;
	private String rightCreateTime;
	private String leftPayTime;
	private String rightPayTime;
	
	private String fileName;
	private byte[] bytes;
	
	public String getFileName() {
		return fileName;
	}

	public void setSpid(String spid) {
		this.spid = spid;
	}

	public void setPtid(String ptid) {
		this.ptid = ptid;
	}

	public void setProductname(String productname) {
		this.productname = productname;
	}

	public void setOrderStatus(String orderStatus) {
		this.orderStatus = orderStatus;
	}

	public void set_mobile(String _mobile) {
		this._mobile = _mobile;
	}

	public void setTerminal(String terminal) {
		this.terminal = terminal;
	}

	public void setLeftCreateTime(String leftCreateTime) {
		this.leftCreateTime = leftCreateTime;
	}

	public void setRightCreateTime(String rightCreateTime) {
		this.rightCreateTime = rightCreateTime;
	}

	public void setLeftPayTime(String leftPayTime) {
		this.leftPayTime = leftPayTime;
	}

	public void setRightPayTime(String rightPayTime) {
		this.rightPayTime = rightPayTime;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	@Override
	public String show() {
		return null;
	}
	
}
